# 1.拿到contid
# 2.拿到videostatus返回的json --》srcurl
# 3.对srcurl里的内容进行修改

import requests

url = 'https://www.pearvideo.com/video_1756772'
contid = url.split('_')[1]
videostatusurl = f'https://www.pearvideo.com/videoStatus.jsp?contId={contid}&mrd=0.4377952043933908'
# 发现User-Agent，cookie不能解决反爬问题
# 因为有防盗链
# 防盗链：请求videostatusurl时，网页会进行溯源，发现源头找不到东西，说明是一个有问题的请求
heard = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.77",
    # 防盗链
    "Referer": "https://www.pearvideo.com/video_1756772"
}

resp = requests.get(videostatusurl,headers=heard)
dic = resp.json()
srcUrl = dic['videoInfo']['videos']['srcUrl']
systemTime = dic['systemTime']
# 真正的视频链接 ：https://video.pearvideo.com/mp4/adshort/20220330/cont-1756772-15852770_adpkg-ad_hd.mp4
# 得到的视频链接 ：https://video.pearvideo.com/mp4/adshort/20220330/1659255778951-15852770_adpkg-ad_hd.mp4
str = 'cont-'  + contid
srcUrl = srcUrl.replace(systemTime,str)
print(srcUrl)
# 4.下载视频
with open('../梨视频/'+srcUrl.split('/')[-1],'wb') as f:
    f.write(requests.get(srcUrl).content)
print('over!')
